/*     */ package handling.netty;
/*     */
/*     */ import client.MapleClient;
/*     */ import configs.OpcodeConfig;
/*     */ import configs.ServerConfig;
/*     */ import handling.MapleServerHandler;
/*     */ import handling.login.handler.LoginPasswordHandler;
/*     */ import handling.opcode.RecvPacketOpcode;
/*     */ import io.netty.buffer.ByteBuf;
/*     */ import io.netty.channel.ChannelHandlerContext;
/*     */ import io.netty.handler.codec.ByteToMessageDecoder;
/*     */ import io.netty.util.AttributeKey;
/*     */ import java.util.List;
/*     */ import org.apache.logging.log4j.LogManager;
/*     */ import org.apache.logging.log4j.Logger;
/*     */ import tools.HexTool;
/*     */ import tools.MapleAESOFB;
/*     */ import tools.StringUtil;
/*     */ import tools.data.input.*;
/*     */
/*     */
/*     */
/*     */
/*     */

/*     */
/*     */ public class MaplePacketDecoder extends ByteToMessageDecoder {
    /*  28 */   public static final AttributeKey<DecoderState> DECODER_STATE_KEY = AttributeKey.newInstance(MaplePacketDecoder.class.getName() + ".STATE");
    /*     */
    /*  32 */   private static final Logger log = LogManager.getLogger("DebugWindows");
    /*     */
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List message) throws Exception {
        MapleClient client = (MapleClient)ctx.channel().attr(MapleClient.CLIENT_KEY).get();
        handling.netty.MaplePacketDecoder.DecoderState decoderState = (handling.netty.MaplePacketDecoder.DecoderState)ctx.channel().attr(DECODER_STATE_KEY).get();
        if (decoderState == null) {
            decoderState = new DecoderState();
            ctx.channel().attr(DECODER_STATE_KEY).set(decoderState);
        }

        int packetLen;
        int pHeader;
        if (in.readableBytes() >= 4 && decoderState.packetlength == -1) {
            int packetHeader = in.readInt();
            packetLen = packetHeader >> 16 & '\uffff';
            if (!client.isLoggedIn() && packetLen == 26985) {
                pHeader = ((packetHeader & 255) << 8) + (packetHeader >> 8 & 255);
                byte[] packet = new byte[pHeader];
                in.readBytes(packet);
                SeekableLittleEndianAccessor slea = new GenericSeekableLittleEndianAccessor(new ByteArrayByteStream(packet));
                LoginPasswordHandler.handlePacket(slea, client);
                return;
            }

            if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
                ctx.channel().disconnect();
                return;
            }

            decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
        } else if (in.readableBytes() < 4 && decoderState.packetlength == -1) {
            return;
        }

        if (in.readableBytes() >= decoderState.packetlength) {
            byte[] decryptedPacket = new byte[decoderState.packetlength];
            in.readBytes(decryptedPacket);
            decoderState.packetlength = -1;
            client.getReceiveCrypto().crypt(decryptedPacket);
            packetLen = decryptedPacket.length;
            pHeader = this.readFirstShort(decryptedPacket);
            if (pHeader >= RecvPacketOpcode.BEGIN_USER.getValue() && client.mEncryptedOpcode.containsKey(pHeader)) {
                pHeader = (Integer)client.mEncryptedOpcode.get(pHeader);
            }

            decryptedPacket[0] = (byte)(pHeader & 255);
            decryptedPacket[1] = (byte)(pHeader >>> 8 & 255);
            message.add(decryptedPacket);
            String pHeaderStr = Integer.toHexString(pHeader).toUpperCase();
            pHeaderStr = StringUtil.getLeftPaddedStr(pHeaderStr, '0', 4);
            String op = this.lookupSend(pHeader);
            if (ServerConfig.DEBUG_MODE && !OpcodeConfig.isblock(op, false)) {
                StringBuilder recvString = new StringBuilder();
                recvString.append("Recv ").append(op).append("[").append(pHeaderStr).append("] (").append(packetLen).append(")").append(client.getPlayer() != null ? " From : " + client.getPlayer().getName() : "").append("\r\n");
                recvString.append(HexTool.toString(decryptedPacket)).append("\r\n");
                if (!op.equals("CLOSE_RANGE_ATTACK") && !op.equals("RANGED_ATTACK") && !op.equals("MAGIC_ATTACK") && !op.equals("SUMMON_ATTACK")) {
                    if (op.equals("SPECIAL_MOVE")) {
                        MapleServerHandler.BuffPacketLog.info(recvString);
                    }
                } else {
                    MapleServerHandler.AttackPakcetLog.info(recvString);
                }

                recvString.append(HexTool.toStringFromAscii(decryptedPacket));
                if (ServerConfig.DEBUG_MODE) {
                    MapleServerHandler.AllPacketLog.info(recvString);
                }
            }
        }

    }

    /*     */   private String lookupSend(int val) {
        /* 109 */     for (RecvPacketOpcode op : RecvPacketOpcode.values()) {
            /* 110 */       if (op.getValue() == val)
                /* 111 */         return op.name();
            /*     */     }
        /* 114 */     return "UNKNOWN";
        /*     */   }
    /*     */
    /*     */   private int readFirstShort(byte[] arr) {
        /* 118 */     return (new GenericLittleEndianAccessor((ByteInputStream)new ByteArrayByteStream(arr))).readShort();
        /*     */   }
    /*     */
    /*     */   private static class DecoderState {
        /*     */     private DecoderState() {}
        /*     */
        /* 122 */     public int packetlength = -1;
        /*     */   }
    /*     */ }

